/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.model;

// imports:
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

import edu.pku.sei.modeler.model.core.MetaClassifier;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.core.MetaStructuralFeature;
import edu.pku.sei.modeler.model.xml.XMLUtility;

/**
 * @generated
 * 
 */
public class Key extends MetaModelElement {
	// attributes
	private MetaClassifier keyClass;

	private List keyAttributes = new ArrayList();

	public MetaClassifier getKeyClass() {
		return keyClass;
	}

	public void setKeyClass(MetaClassifier keyClass) {
		this.keyClass = keyClass;
	}

	public List getKeyAttributes() {
		return keyAttributes;
	}

	public void setKeyAttributes(List keyAttributes) {
		this.keyAttributes = keyAttributes;
	}

	// references

	// getters & setters for attributes

	// getters for references

	// save & load
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodeNotContain(root,
				ModelPackage.KEY_CLASS, this.keyClass);
		XMLUtility.xmlInstance.serializeNodesNotContain(root,
				ModelPackage.KEY_ATTRIBUTES, this.getKeyAttributes());
	}

	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.keyClass = (MetaClassifier) XMLUtility.xmlInstance
				.deSerializeNodeNotContain(root, ModelPackage.KEY_CLASS, this);
		this.getKeyAttributes().addAll(
				XMLUtility.xmlInstance.deSerializeNodesNotContain(root,
						ModelPackage.KEY_ATTRIBUTES, this));

	}

	public void dynamicSet(String featureID, Object value) {
		if (featureID == null || "".equals(featureID)) {
			return;
		} else if (ModelPackage.KEY_CLASS.equals(featureID)) {
			this.keyClass = (MetaClassifier) value;
		} else if (ModelPackage.KEY_ATTRIBUTES.equals(featureID)) {
			this.keyAttributes.add(value);
		} else
			super.dynamicSet(featureID, value);
	}

	@Override
	public String toString() {
		String key = "empty";
		if (this.keyClass != null) {
			key = this.keyClass.getName() + "(";
			for (int i = 0; i < this.keyAttributes.size(); i++) {
				key += ((MetaStructuralFeature) keyAttributes.get(i)).getName();
				if (i == this.keyAttributes.size() - 1) {
					key += ')';
				} else {
					key += ',';
				}
			}
		}
		return key;
	}
}
